<template>
  <div>
    <!-- 增加查询重置组件 -->
    <!-- inline="true" 表单一行显示 -->
    <el-form
      ref="searchForm"
      :inline="true"
      :model="searchMap"
      class="demo-form-inline"
      style="margin-left:60px"
    >
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="供应商名称"></el-input>
      </el-form-item>

      <el-form-item prop="linkman">
        <el-input v-if="!isDialog" v-model="searchMap.linkman" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input v-if="!isDialog" v-model="searchMap.mobile" placeholder="联系电话"></el-input>
      </el-form-item>

      <el-form-item style="margin-left:40px">
        <el-button type="primary" @click="fetchData">查询</el-button>
        <el-button v-if="!isDialog" type="primary" @click="addMember">新增</el-button>
        <el-button v-if="!isDialog" @click="resetForm('searchForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格组件 -->
    <!-- highlight-current-row激活单选 -->
    <!-- current-change这个事件 当点击某一行之后就会触发这个事件 调用handleCurrentChange事件处理函数
    并且会接受两个参数currentRow，oldCurrentRow -->
    <el-table
      :data="list"
      height="435"
      border
      style="width:100%"
      :highlight-current-row="isDialog"
      @current-change="handleCurrentChangeRows"
    >
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="name" label="供应商名称"></el-table-column>
      <el-table-column prop="linkman" label="联系人"></el-table-column>
      <el-table-column v-if="!isDialog" prop="mobile" label="联系电话"></el-table-column>
      <el-table-column v-if="!isDialog" prop="remark" label="备注"></el-table-column>
      <el-table-column v-if="!isDialog" label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10,20,30]"
      :page-size="pageSize"
      :layout="isDialog ? 'prev, pager, next':'total, sizes, prev, pager,next, jumper'"
      :total="total"
      style="margin-left:60px"
    ></el-pagination>

    <!-- 弹出供应商编辑模块 -->
    <el-dialog v-if="!isDialog" title="供应商编辑" :visible.sync="dialogFormVisible" width="500px">
      <el-form
        :rules="rules"
        :model="pojo"
        ref="form"
        label-width="100px"
        label-position="right"
        style="width:400px"
      >
        <el-form-item label="供应商名称" prop="name">
          <el-input v-model="pojo.name"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="linkman">
          <el-input v-model="pojo.linkman"></el-input>
        </el-form-item>

        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="pojo.mobile"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="pojo.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" :plain="true" @click="submit('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//导入默认对象
import supplierApi from "@/api/supplier";
export default {
  props: {
    isDialog: Boolean
  },
  data() {
    return {
      list: [],
      searchMap: {
        name: "",
        linkman: "",
        mobile: null
      },
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogFormVisible: false,
      pojo: {
        name: "",
        linkman: "",
        mobile: null,
        remark: ""
      },
      //校验规则
      rules: {
        name: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        linkman: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    //抓取供应商列表数据和查询供应商信息
    fetchData() {
      supplierApi
        .search(this.currentPage, this.pageSize, this.searchMap)
        .then(response => {
          // console.log(response.data.data);
          const getSprData = response.data;
          this.list = getSprData.data.rows;
          this.total = getSprData.data.total;
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },

    addMember() {
      this.dialogFormVisible = true;

      //重置表单
      this.$nextTick(() => {
        this.$refs["form"].resetFields();
      });
    },
    //新增供应商数据
    submit(formName) {
      this.$refs[formName].validate(valid => {
        //校验通过
        if (valid) {
          //如果pojo对象中有ID字段说明为更新提交
          if (this.pojo.id) {
            supplierApi.upDate(this.pojo.id, this.pojo).then(response => {
              const updatedResData = response.data;
              //如果更新成功
              if (updatedResData.flag) {
                this.$message({
                  message: updatedResData.message,
                  type: "success"
                });
                this.fetchData();
                this.dialogFormVisible = false;

                //更新失败
              } else {
                this.$message.error(updatedResData.message);
                this.fetchData();
                this.dialogFormVisible = true;
              }
            });
            // 为了区分是更新提交还是增加提交
            delete this.pojo.id;
            //如果pojo没有ID字段就认为新增提交
          } else {
            supplierApi.add(this.pojo).then(response => {
              const addSprResponseData = response.data;
              //添加成功
              if (addSprResponseData.flag) {
                // 弹出提示信息
                this.$message({
                  message: addSprResponseData.message,
                  type: "success"
                });
                //重新抓取数据
                this.fetchData();
                //退出编辑对话框
                this.dialogFormVisible = false;
                //添加失败
              } else {
                this.$message.error(addSprResponseData.message);
                this.dialogFormVisible = true;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    //重置表单
    resetForm(formName) {
      //重置表单必须给每个组件标签<el-form-item> 添加prop属性并且指定字段
      this.$refs[formName].resetFields();
    },
    //通过ID查询供应商数据
    handleEdit(id) {
      this.dialogFormVisible = true;
      supplierApi.geSprMessageByID(id).then(response => {
        const queriedResponseData = response.data;
        //查询成功
        if (queriedResponseData.flag) {
          this.pojo = queriedResponseData.data;
          //查询失败
        } else {
          this.addMember();
          this.$message.error(queriedResponseData.message);
        }
      });
    },
    //删除数据
    handleDelete(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          supplierApi.delData(id).then(response => {
            const deletedResponseMessage = response.data.message;
            //如果删除成功
            if (response.data.flag) {
              this.$message({
                message: deletedResponseMessage,
                type: "success"
              });
              this.fetchData();
              //如果删除失败
            } else {
              this.$message({
                showClose: true,
                message: deletedResponseMessage,
                type: "error"
              });
              this.fetchData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //当点击某一行时 就会触发该事件处理函数
    handleCurrentChangeRows(currentRow,){
        
      // console.log(currentRow);
      this.$emit('option-supplier',currentRow);
    }
  }
};
</script>